package com.taoyuanx.boot.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.taoyuanx.boot.business.TokenBusiness;
import com.taoyuanx.boot.common.Result;
import com.taoyuanx.boot.service.IAppService;

/**
 * @author 都市桃源
 * api 适配器 空实现
 */
public abstract class ApiAdapter implements Api {
	@Autowired
	protected TokenBusiness tokenBusiness;
	@Autowired
	protected IAppService appService;
	@Override
	public Result hello(JSONObject param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result authToken(JSONObject param) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result refreshToken(JSONObject param) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getClientIP(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		 String ip = request.getHeader("x-forwarded-for");
		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
		        ip = request.getHeader("Proxy-Client-IP");
		    }
		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
		        ip = request.getHeader("WL-Proxy-Client-IP");
		    }
		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
		        ip = request.getRemoteAddr();
		    }
		    return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
	}

}
